from lin_base import *

def ecl_seg_day_xy(year,month,day,tz):
    nd = cal_d_w_hm(year,month,day,-tz,0)
    ra_deg,dec,_ = SunValues(nd)
    ra = ra_deg/15.0
    if ra < g_share.prev_ra:
        ra +=24
    ey = dec_to_y(dec)
    ex = RA_to_x(g_share.x0, ra)
    g_share.prev_ra = ra
    if ex > g_share.xr_end:
        return None,None
    return ex,ey

def ecl_seg_xys(year,month,day0,day1,tz):
    ex0,ey0 = ecl_seg_day_xy(year,month,day0,tz)
    ex1,ey1 = ecl_seg_day_xy(year,month,day1,tz)
    if ex0 is None or ex1 is None:
        return None
    return ((ex0,ey0), (ex1,ey1))

def draw_sky_ecl(paper, s_year, tz):
    layer= paper.add_layer(name='sky_ecl')
    draw = layer.draw
    month=12
    year=s_year -1
    g_share.prev_ra=0
    exys = ecl_seg_xys(year,month,25,28,tz)
    print('exys:',exys)
    draw.line(exys, fill=config.ECL_LINE_COLOR, 
              width=config.ECL_LINE_WIDTH)
    
    for dn in range(1,367):
        month,day,year = days_to_date(dn, s_year)
        if day not in [1,6,11,16,21,26]:
            continue
        exys = ecl_seg_xys(year,month,day,day+2,tz)
        if exys is None:
            break
        #print('exys:',exys)
        draw.line(exys, fill=config.ECL_LINE_COLOR, 
              width=config.ECL_LINE_WIDTH)

    for dn in range(1,60):
        month,day,year = days_to_date(dn, s_year+1)
        if day not in [1,6,11,16,21,26]:
            continue
        exys = ecl_seg_xys(year,month,day,day+2,tz)
        if exys is None:
            break
        #print('exys:',exys)
        draw.line(exys, fill=config.ECL_LINE_COLOR, 
              width=config.ECL_LINE_WIDTH)
